package com.benjaminwan.read.free.model.moshiadapter

import com.benjaminwan.read.free.model.action.*
import com.benjaminwan.read.free.model.enums.ActionType
import com.benjaminwan.read.free.utils.fromJsonToActionByMoshi
import com.benjaminwan.read.free.utils.fromJsonToAnyByMoshi
import com.benjaminwan.read.free.utils.toActionJsonByMoshi
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson

object ActionAdapter {
    @ToJson
    fun toJson(action: Action): String {
        return when (action) {
            is Delay -> {
                toActionJsonByMoshi(action)
            }
            is KeyboardClick -> {
                toActionJsonByMoshi(action)
            }
            is MouseDragPage -> {
                toActionJsonByMoshi(action)
            }
            is MouseButtonClick -> {
                toActionJsonByMoshi(action)
            }
            is MouseButtonDown -> {
                toActionJsonByMoshi(action)
            }
            is MouseButtonUp -> {
                toActionJsonByMoshi(action)
            }
            is KeyboardKeyDown -> {
                toActionJsonByMoshi(action)
            }
            is KeyboardKeyUp -> {
                toActionJsonByMoshi(action)
            }
            is MouseScroll -> {
                toActionJsonByMoshi(action)
            }
            is MouseMove -> {
                toActionJsonByMoshi(action)
            }
            is MouseMoveLong -> {
                toActionJsonByMoshi(action)
            }
            else -> ""
        }
    }

    @FromJson
    fun fromJson(action: String): Action? {
        //Logger.i(action)
        val ret = fromJsonToAnyByMoshi<ActionImpl>(action) ?: return null
        //Logger.i(ret.toString())
        return when (ret.actionType) {
            ActionType.Delay -> {
                fromJsonToActionByMoshi<Delay>(action)
            }
            ActionType.KeyboardClick -> {
                fromJsonToActionByMoshi<KeyboardClick>(action)
            }
            ActionType.MouseDragPage -> {
                fromJsonToActionByMoshi<MouseDragPage>(action)
            }
            ActionType.MouseButtonClick -> {
                fromJsonToActionByMoshi<MouseButtonClick>(action)
            }
            ActionType.MouseButtonDown -> {
                fromJsonToActionByMoshi<MouseButtonDown>(action)
            }
            ActionType.MouseButtonUp -> {
                fromJsonToActionByMoshi<MouseButtonUp>(action)
            }
            ActionType.KeyboardKeyDown -> {
                fromJsonToActionByMoshi<KeyboardKeyDown>(action)
            }
            ActionType.KeyboardKeyUp -> {
                fromJsonToActionByMoshi<KeyboardKeyUp>(action)
            }
            ActionType.MouseScroll -> {
                fromJsonToActionByMoshi<MouseScroll>(action)
            }
            ActionType.MouseMove -> {
                fromJsonToActionByMoshi<MouseMove>(action)
            }
            ActionType.MouseMoveLong -> {
                fromJsonToActionByMoshi<MouseMoveLong>(action)
            }
            else -> ret
        }

    }

}